package com.chenl.provider.DEMOS.Spring.Spring事务相关;

import com.chenl.provider.DTO.entity.MyStudent;
import com.chenl.provider.service.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class SpringTransService {
    @Autowired
    StudentServiceImpl studentService;

    @Transactional
    public void save(){
        TransactionUtils.doAfterTransaction(() -> {
            log.info("事务提交了,发送成功MQ~~然后开始睡觉2");
            try {
                TimeUnit.SECONDS.sleep(5);
                log.info("事务提交了,发送成功MQ~~睡觉结束2");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },()->{
            log.info("事务回滚了,发送失败MQ~~2");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        MyStudent student = new MyStudent();
        student.setStuName("张三1");
        student.setStuAge("22");
        student.setPhone("17720885142");
        studentService.save(student);
    }
}
